package org.tlipoca.smuserdirectory.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.genntii.smcommon.constant.GatewayConstant;
import org.genntii.smcommon.result.Result;
import org.genntii.smdomain.dto.ArticleFavoritesDTO;
import org.genntii.smdomain.dto.UserDirectoryDTO;
import org.genntii.smdomain.entity.UserDirectory;
import org.springframework.web.bind.annotation.*;
import org.tlipoca.smuserdirectory.service.UserDirectoryService;

import java.util.ArrayList;

@Tag(name = "用户收藏夹")
@Slf4j
@RestController
@RequestMapping("/user_directory")
public class UserDirectoryController {

    @Resource
    private UserDirectoryService userDirectoryService;

    @Operation(summary = "获取用户收藏夹是否存在")
    @GetMapping("/exists")
    public Result<Boolean> getUserDirectoryExists(@RequestParam("directoryId") Long directoryId) {
        return Result.success(userDirectoryService.getUserDirectoryExistsService(directoryId));
    }

    @Operation(summary = "获取用户收藏夹")
    @GetMapping
    public Result<ArrayList<UserDirectory>> getUserDirectory(HttpServletRequest request) {
        Long userId = Long.valueOf(request.getHeader(GatewayConstant.USER_ID));
        return Result.success(userDirectoryService.getUserDirectoryService(userId));
    }

    @Operation(summary = "新增用户收藏夹")
    @PostMapping
    public Result addUserDirectory(@RequestBody UserDirectoryDTO userDirectoryDTO, @RequestHeader(GatewayConstant.USER_ID) String id) {
        Long userId = Long.valueOf(id);
        userDirectoryService.addUserDirectoryService(userDirectoryDTO,userId);
        return Result.success();
    }

    @Operation(summary = "新建用户时自动创建默认文件夹")
    @PostMapping("default")
    public Result addDefaultFavorite(@RequestBody UserDirectoryDTO userDirectoryDTO, @RequestParam("userId") Long userId){
        userDirectoryService.addUserDirectoryService(userDirectoryDTO,userId);
        return Result.success();
    }

    @Operation(summary = "删除用户收藏夹")
    @DeleteMapping("/{directoryId}")
    public Result deleteUserDirectory(@PathVariable Long directoryId,HttpServletRequest request) {
        Long userId = Long.valueOf(request.getHeader(GatewayConstant.USER_ID));
        userDirectoryService.deleteUserDirectoryService(directoryId,userId);
        return Result.success();
    }

    @Operation(summary = "批量删除用户收藏夹")
    @DeleteMapping
    public Result deleteUserDirectoryBatch(@RequestParam ArrayList<Long> directoryIds,HttpServletRequest request) {
        Long userId = Long.valueOf(request.getHeader(GatewayConstant.USER_ID));
        userDirectoryService.deleteUserDirectoryBatchService(directoryIds,userId);
        return Result.success();
    }

    @Operation(summary = "更新用户收藏夹")
    @PutMapping("/{directoryId}")
    public Result updateUserDirectory(@RequestBody UserDirectoryDTO userDirectoryDTO, @PathVariable Long directoryId,HttpServletRequest request) {
        Long userId = Long.valueOf(request.getHeader(GatewayConstant.USER_ID));
        userDirectoryService.updateUserDirectoryService(userDirectoryDTO, directoryId,userId);
        return Result.success();
    }

    @Operation(summary = "更新收藏夹状态")
    @PutMapping("/status/{directoryId}")
    public Result updateUserDirectoryStatus(@PathVariable Long directoryId, Integer status) {
        userDirectoryService.updateUserDirectoryStatusService(directoryId, status);
        return Result.success();
    }
}
